// pages/album/create.js
import WxNotificationCenter from '../../utils/WxNotificationCenter'

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickname: '',
    albumName: '',
    birthday: '',
    gender: 1,
    intro: '',
    avatarPath: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    app.getUploadToken(function(err, token) {
      if (!err) {
        that.setData({
          upToken: token
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },


  /**
   * 创建相册
   * 提交相关信息
   */
  createAlbum: function () {

    // 验证数据完整
    if (!this.data.avatarPath) {
      return wx.showToast({
        title: '请添加相册图标',
        image: '/images/icon/icon_attention.png'
      })
    }
    if (!this.data.nickname) {
      return wx.showToast({
        title: '名字不能为空',
        image: '/images/icon/icon_attention.png'
      })
    }
    if (!this.data.birthday) {
      return wx.showToast({
        title: '生日不能为空',
        image: '/images/icon/icon_attention.png'
      })
    }

    if (!this.data.albumName) {
      return wx.showToast({
        title: '相册名不能为空',
        image: '/images/icon/icon_attention.png'
      })
    }

    if (!this.data.intro) {
      return wx.showToast({
        title: '相册描述不能为空',
        image: '/images/icon/icon_attention.png'
      })
    }

    let that = this
    let url = app.globalData.apiHost + '/albums/'
    let formData = {
      baby_name: this.data.nickname,
      baby_gender: this.data.gender,
      baby_birthday: this.data.birthday,
      title: this.data.albumName,
      intro: this.data.intro
    }
    wx.showLoading({
      mask: true,
      title: '正在创建...',
    })
    wx.uploadFile({
      url: url,
      filePath: this.data.avatarPath,
      name: 'icon',
      header: {
        'token': app.globalData.loginToken
      },
      formData: formData,
      success: function (res) {
        wx.hideLoading()
        console.log(res)
        if (res.statusCode >= 200 && res.statusCode < 300) {
          let data = JSON.parse(res.data)
          app.globalData.albums.push(data)
          WxNotificationCenter.postNotificationName('refreshAlbums')
          // 创建成功，跳转到创建成功页面
          wx.redirectTo({
            url: '/pages/album/index?id=' + data.id,
          })
        } else {
          wx.showToast({ title: '保存失败，请重试' })
        }
      },
      fail: function (err) {
        console.log(err)
        wx.hideLoading()
        wx.showToast({
          title: '保存失败，请重试',
        })
      }
    })
  },

  /*
   * 名字修改
   */
  nicknameChange: function(e) {
    this.setData({
      nickname: e.detail.value
    })
  },

  /*
   * 名字确认
   */
  nicknameConfirm: function(e) {
    if (!this.data.albumName) {
      this.setData({
        albumName: e.detail.value + '的相册'
      })
    }
  },

  /*
   * 相册名字修改
   */
  albumNameChange: function (e) {
    this.setData({
      albumName: e.detail.value
    })
  },

  /*
   * 相册名字修改
   */
  introChange: function (e) {
    this.setData({
      intro: e.detail.value
    })
  },

  /*
   * 选择宝宝生日
   */
  bindDateChange: function (e) {
    this.setData({
      birthday: e.detail.value
    })
  },

  /*
   * 选择宝宝性别
   */
  chooseGender: function() {
    let that = this
    wx.showActionSheet({
      itemList: ['未知', '小王子', '小公主'],
      success: function (res) {
        that.setData({
          gender: res.tapIndex
        })
      }
    })
  },

  /*
   * 选择宝宝相册头像
   */
  chooseAvatar: function () {
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      success: function(res) {
        that.setData({
          avatarPath: res.tempFilePaths[0]
        })
      },
    })
  }
})